<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    :title="isEdit ? '修改考评周期' : '新增考评周期'"
    width="800px"
  >
    <el-form :model="form" ref="form" label-width="170px" :rules="rules">
      <el-form-item label="周期名称" prop="moralityCycleName">
        <el-input v-model="form.moralityCycleName" placeholder="请输入周期名称" />
      </el-form-item>
      <el-form-item label="考评开始时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.cycleStartDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="考评结束时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.cycleEndDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="调取加减分项目开始时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.rewardStartDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="调取加减分项目结束时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.rewardEndDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="自评开始时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.selfStartDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="自评结束时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.selfEndDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="科室考评开始时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.deptStartDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="科室考评结束时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.deptEndDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="党支部考评开始时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.branchStartDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="党支部考评结束时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.branchEndDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="考评委员会考评开始时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.committeeStartDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="考评委员会考评结束时间">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="form.committeeEndDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSure">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { cycleCreate, cycleUpdate } from '@/api/morality'
import { openNotification } from '@/utils/util'

export default {
  data() {
    return {
      visible: false,
      isEdit: false,
      form: {
        moralityCycleName: null,
        cycleStartDate: null,
        cycleEndDate: null,
        selfStartDate: null,
        selfEndDate: null,
        rewardStartDate: null,
        rewardEndDate: null,
        deptStartDate: null,
        deptEndDate: null,
        branchStartDate: null,
        branchEndDate: null,
        committeeStartDate: null,
        committeeEndDate: null
      },
      selectData: [],
      rules: {
        moralityCycleName: [{ required: true, message: '请输入周期名称', trigger: 'blur' }],
        moralityItemType: [{ required: true, message: '请选择项目类型', trigger: 'blur' }],
        leaf: [{ required: true, message: '请选择是否末级', trigger: 'blur' }],
        itemOrder: [{ type: 'number', message: '请输入数字', trigger: 'blur' }],
        itemStandard: [{ type: 'number', message: '请输入数字', trigger: 'blur' }]
      }
    }
  },
  mounted() {},
  methods: {
    edit(bool, obj) {
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, obj)
    },
    handleSure() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = '',
            mes = ''
          if (this.isEdit) {
            mes = '修改操作成功'
            res = await cycleUpdate(this.form)
          } else {
            mes = '新增操作成功'
            res = await cycleCreate(this.form)
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.el-date-editor {
  width: 100%;
}
</style>